<?php

/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 7/8/14
 * Time: 8:51 AM
 */
class GetTigerDirectCommand extends CConsoleCommand {
    public function run($args) {
        $url = "http://www.tigerdirect.com/xml/rsstigercat1.xml";
        $results = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-type: text/xml'
        ));
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 400);

        $content = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if (gettype($content) == "string") {
            $x = new SimpleXmlElement($content);

            $arr = array();
            foreach ($x->channel->item as $entry) {
                $deal = new Deal();
                $deal->name = $entry->title;
                $deal->url = $entry->link;

                $arr = HSCommon::handleArray(array_filter(explode('- $', $entry->title)));
                end($arr);
                $key = key($arr);
                $price = "$" . $arr[$key];

                $deal->price = trim($price);
                $deal->picture = '';
                $deal->created_date = date('d-m-Y');
                $deal->type = DealTypeEnum::tiger_direct;

                $old_deal = Deal::model()->findByAttributes(array(
                    'name' => $deal->name,
//                    'created_date' => $deal->created_date,
                    'type' => $deal->type
                ));

                if ($old_deal != null) continue;

                if ($deal->save()) {
                    array_push($results, $deal);
                }
            }

            if (count($results) > 0 && count($results) > Yii::app()->params['limit']) {
                $html = '';
                $html .= '<table style="width:100%;" border="1">';
                $html .= '<tr>';
                $html .= '<th>Picture</th>';
                $html .= '<th>Name</th>';
                $html .= '<th>Price</th>';
                $html .= '<th>Link</th>';
                $html .= '</tr>';
                foreach ($results as $result) {

                    $html .= '<tr>';
                    if ($result->picture != '') {
                        $html .= '<td style="text-align:center"><img style="max-width: 100px; max-height: 100px" alt="" src="' . $result->picture . '"/> </td>';
                    } else {
                        $html .= '<td> </td>';
                    }
                    $html .= '<td> ' . $result->name . '</td>';
                    $html .= '<td>' . $result->price . '</td>';
                    $html .= '<td><a href="' . $result->url . '">Link</a></td>';
                    $html .= '</tr>';

                }
                $html .= '</table>';

                Mailer::sendEmail(Yii::app()->params['large_email'], "TigerDirect's deals", $html);
            } else {
                foreach ($results as $result) {
                    $html = '';
                    $html .= '<p>';
                    $html .= "<div>Tên: " . $result->name . "</div>";
                    $html .= "<div>Giá: " . $result->price . "</div>";
                    $html .= "<div>Link chi tiết: <a href=" . $result->url . ">Link</a></div>";
                    $html .= '</p>';
                    Mailer::sendEmail(Yii::app()->params['small_email'], "$result->name | $result->price", $html);
                }
            }
        }
    }
}